package com.blq.web.PayoutInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blq.common.utils.ResultMsg;
import com.blq.common.utils.StringUtils;
import com.blq.common.core.page.TableDataInfo;
import com.blq.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.blq.web.PayoutInformation.domain.bo.PayoutInformationBo;
import com.blq.web.PayoutInformation.domain.vo.PayoutInformationVo;
import com.blq.web.PayoutInformation.domain.PayoutInformation;
import com.blq.web.PayoutInformation.mapper.PayoutInformationMapper;
import com.blq.web.PayoutInformation.service.IPayoutInformationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 收款信息Service业务层处理
 *
 * @author Blq
 * @date 2022-09-13
 */
@RequiredArgsConstructor
@Service
public class PayoutInformationServiceImpl extends ServiceImpl<PayoutInformationMapper,PayoutInformation> implements IPayoutInformationService {

    private final PayoutInformationMapper baseMapper;


    /**
     * 查询收款信息
     */
    @Override
    public PayoutInformationVo queryById(String payoutKey){
        return baseMapper.selectVoById(payoutKey);
    }

    /**
     * 查询收款信息列表
     */
    @Override
    public TableDataInfo<PayoutInformation> queryPageList(PayoutInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayoutInformation> lqw = buildQueryWrapper(bo);
//        Page<PayoutInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<PayoutInformation> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询收款信息列表
     */
    @Override
    public List<PayoutInformationVo> queryList(PayoutInformationBo bo) {
        LambdaQueryWrapper<PayoutInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayoutInformation> buildQueryWrapper(PayoutInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayoutInformation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPayoutFrom()), PayoutInformation::getPayoutFrom, bo.getPayoutFrom());
        lqw.eq(StringUtils.isNotBlank(bo.getPayoutUser()), PayoutInformation::getPayoutUser, bo.getPayoutUser());
        lqw.eq(StringUtils.isNotBlank(bo.getPayoutPhone()), PayoutInformation::getPayoutPhone, bo.getPayoutPhone());
        lqw.eq(bo.getPayoutPrice() != null, PayoutInformation::getPayoutPrice, bo.getPayoutPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getPayoutCode()), PayoutInformation::getPayoutCode, bo.getPayoutCode());
        return lqw;
    }

    /**
     * 新增收款信息
     */
    @Override
    public Boolean insertByBo(PayoutInformationBo bo) {
        PayoutInformation add = BeanUtil.toBean(bo, PayoutInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPayoutKey(add.getPayoutKey());
        }
        return flag;
    }

    /**
     * 修改收款信息
     */
    @Override
    public Boolean updateByBo(PayoutInformationBo bo) {
        PayoutInformation update = BeanUtil.toBean(bo, PayoutInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayoutInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除收款信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
